package managedbean.CRM.SaleOrder;

import entity.CRM.LineItem;
import entity.CRM.ProductPrice;
import entity.CRM.SaleOrder;
import java.io.IOException;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.FRM.CreditAccountBeanLocal;
import session.stateless.CRM.DiscountBeanLocal;
import session.stateless.CRM.ProductPriceBeanLocal;
import session.stateless.CRM.SaleInquiryBeanLocal;
import session.stateless.CRM.SaleOrderBeanLocal;

@ManagedBean(name="editSaleOrderManagerBean")
@ViewScoped

public class EditSaleOrderManagerBean implements Serializable {
    
    public EditSaleOrderManagerBean () {
    }
    
    @EJB
    private SaleOrderBeanLocal saleOrderBeanLocal;
    @EJB
    private CreditAccountBeanLocal creditAccountBeanLocal;
    @EJB
    private ProductPriceBeanLocal productBeanLocal;
    @EJB
    private DiscountBeanLocal discountBeanLocal;
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
    
    private SaleOrder selectedSaleOrder;
    private List<LineItem> lineItems;
    private Double finalPrice;
    private String statusMessage;

    public void initView(PhaseEvent event)
    {        
        if (selectedSaleOrder == null)                        
            selectedSaleOrder = (SaleOrder) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedSaleOrder");                    
        
        this.setLineItems(selectedSaleOrder.getLineItems());                 
        Double temp_finalPrice = new Double(0);
        for (LineItem temp:this.getLineItems())
        {
            temp_finalPrice+=temp.getFinalPrice();
        }
        this.setFinalPrice(temp_finalPrice);        
    }
    
    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public SaleOrder getSelectedSaleOrder() {
        return selectedSaleOrder;
    }

    public void setSelectedSaleOrder(SaleOrder selectedSaleOrder) {
        this.selectedSaleOrder = selectedSaleOrder;
    }
     
    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Double getFinalPrice() {
        return finalPrice;
    }

    public void setFinalPrice(Double finalPrice) {
        this.finalPrice = finalPrice;
    }
    

    public void saveNewSaleOrder(ActionEvent event)throws IOException
    {
        Double customDiscount=this.getSelectedSaleOrder().getSaleQuotation().getCustomDiscount();
        
        List<LineItem> temp_LineItems = new ArrayList<LineItem>();
        for (LineItem temp:this.getLineItems())
        {
            LineItem temp_LineItem=saleInquiryBeanLocal.createLineItem_VolumeCustomDiscountIncluded(temp.getProductPrice(), temp.getLineItemQuantity(), customDiscount);
            temp_LineItems.add(temp_LineItem);
        }
        
        System.out.println("managedBean test b4 update"+temp_LineItems.get(0).getLineItemQuantity());
        
        saleOrderBeanLocal.updateSaleOrder(selectedSaleOrder.getId(), temp_LineItems);
        statusMessage = "Sale Order Updated Successfully.";
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));
    }

    
    public void calculate(ActionEvent event) throws IOException
    {
        Double customDiscount=this.getSelectedSaleOrder().getSaleQuotation().getCustomDiscount();
        Double temp_Price= 0.0;
        for (LineItem lineItem:this.getLineItems())
        {
            ProductPrice temp_product= productBeanLocal.lookupRange(lineItem.getProductPrice().getProductType(), lineItem.getLineItemQuantity());
            System.out.println("test1"+temp_product.getProductType());
            lineItem.setProductPrice(temp_product);
            
            Double temp_discountRate= discountBeanLocal.calculateDiscountRate(lineItem.getProductPrice().getProductType(), lineItem.getLineItemQuantity());
            System.out.println("test2"+temp_discountRate);                    
            lineItem.setVolumeDiscountRate(temp_discountRate);
        
            Double temp_finalPrice=temp_product.getProductPrice() * lineItem.getLineItemQuantity() * (1-(customDiscount+temp_discountRate)/100);
            lineItem.setFinalPrice(temp_finalPrice);
            temp_Price+=temp_finalPrice;
        }
        this.setFinalPrice(temp_Price);
    }
}
